<?php
	function __autoload($class){
		require_once "../".$class.".php";
	}
	
	$validationName = 0;
	$validationPassword = 0;
	$validationEmail = 0;
	$validationTeam = 0;
	
	$managerNameExists = 0;
	$managerEmailExists = 0;
	$teamNameExists = 0;
	
	$manager = new Manager("", "", "");
	$team = new Team("");
	
	if(isset($_POST['addmanager'])){
		$manager = new Manager($_POST['managername'], $_POST['password'], $_POST['email']);
		$team = new Team($_POST['team']);
		
		$validationName = $manager->validationName(trim($_POST['managername']));
		$validationPassword = $manager->validationPassword(trim($_POST['password']));
		$validationEmail = $manager->validationEmail(trim($_POST['email']));
		$validationTeam = $team->validationTeamName(trim($_POST['team']));
		
		$dbconnect = new DBconnect();

		$managerNameExists = $dbconnect->isManagerNameExists($manager);
		$managerEmailExists = $dbconnect->isManagerEmailExists($manager);
		$teamNameExists = $dbconnect->isTeamNameExists($team);
	}
	
	require_once 'adminHeader.php';
?>

	<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' />
	<table>
		<th>Add Manager</th>
		<tr>
			<td>Manager Name</td>
			<td><input type='text' name='managername' id='managername'
				value='<?php echo $manager->getName(); ?>' /></td>
			<td><?php if($validationName) echo "Please, enter name!"; 
					  if($managerNameExists) echo "This name is not availible!";
			?></td>	
		</tr>
		<tr>
			<td>Password</td>
			<td><input type='password' name='password' id='password' /></td>
			<td><?php if($validationPassword) echo "Please, enter valid password!"; ?></td>
		</tr>
		<tr>
			<td>Email</td>
			<td><input type='text' name='email' id='email' value="<?php echo $manager->getEmail(); ?>"/></td>
			<td><?php if($validationEmail) echo "Please, enter valid email address!"; 
			 		  if($managerEmailExists) echo "This email is not availible!";
			?></td>
		</tr>
		<tr>
			<td>Team Name</td>
			<td><input type='text' name='team' id='team'
				value='<?php echo $team->getTeamName(); ?>' /></td>
			<td><?php if($validationTeam) echo "Please, enter team name!"; 
					  if($teamNameExists) echo "This team name is not availible!";
			?></td>	
		</tr>
		<tr>
			<td></td>
			<td><input type='submit' name='addmanager' value="Add Manager" /></td>
		</tr>
	</table>
	</form>
	
		<?php
			if(isset($_POST['addmanager']) && ($validationName + $validationPassword + $validationEmail + $validationTeam ) == 0)
			{
				if($managerNameExists + $managerEmailExists + $teamNameExists == 0){
					$dbconnect->add_manager($manager, $team);
					$dbconnect->add_team($manager, $team);
					echo "Successfully added!";
				}
			}
			require_once 'adminFooter.php';
		?>

